/*
 * @Date: 2025-04-02 21:38:15
 * @LastEditors: 贾二小 erxiao.jia@outlook.com
 * @LastEditTime: 2025-04-24 22:23:43
 * @FilePath: /admin/vue/src/composables/system/useUser.ts
 */
import { http } from '@/plugins/axios'

export default () => {
  const collection = ref<ResponsePageResult<UserModel>>()
  const params = ref({
    key: '',
    page: 1,
    per_page: 15,
  })
  const model = ref<UserModel>()

  const load = async () => {
    collection.value = await http.request<UserModel[], ResponsePageResult<UserModel>>({
      url:
        `user?` +
        Object.entries(params.value)
          .map(([k, v]) => `${k}=${v}`)
          .join('&'),
    })
  }

  const find = async (id: string) => {
    const { data } = await http.request<UserModel>({
      url: `user/${id}`,
    })
    model.value = data
  }

  const add = async (model: UserModel) => {
    const data = await http.request<string, ResponseResult<string>>({
      url: 'user',
      method: 'POST',
      data: model,
    })
    return data
  }

  const update = async (model: UserModel) => {
    const data = await http.request<string, ResponseResult<string>>({
      url: `user/${model.id}`,
      method: 'PUT',
      data: model,
    })
    return data
  }

  const del = async (model: UserModel) => {
    await http.request({ url: `user/${model.id}`, method: 'DELETE', data: model })
    load()
  }

  return { collection, model, load, params, find, add, update, del }
}
